import src.SNMP.Master as mas
import src.SNMP.Node as nod
import src.SNMP.Wrpc as wrpc
from src import Node_raw_data
from src import Master_ip_data
from epics import caput
from src import WR_config,Device_config
from src import check_ping
import time

def Monitor_main():
    time.sleep(3)
    print("Bos_Monitor Thread Start")
    impInterlockReadGroup = mas.impInterlockReadGroup()
    while True:
        time.sleep(2)
        try:
            try:
                M1_data = impInterlockReadGroup.walk_impInterlockReadGroup(Master_ip_data["M1 ip"])
                M1_impInterlock7100 = int(M1_data["impInterlock7100"])
                M1_impInterlock19eg = int(M1_data["impInterlock19eg"])
                M1_impOvertime = int(M1_data["impOvertime"])
                M1_impPhoenix = int(M1_data["impPhoenix"])

            except Exception as e:
                print("M1 状态检测Error:",e)
            try:
                M2_data = impInterlockReadGroup.walk_impInterlockReadGroup(Master_ip_data["M2 ip"])
                M2_impInterlock7100 = int(M2_data["impInterlock7100"])
                M2_impInterlock19eg = int(M2_data["impInterlock19eg"])
                M2_impOvertime = int(M2_data["impOvertime"])
                M2_impPhoenix = int(M2_data["impPhoenix"])

            except Exception as e:
                print("M2 状态检测Error:",e)
            # print("M1:",M1_data,"\nM2:",M2_data)
            try:
                if M1_impInterlock7100 == 1 and M1_impInterlock19eg == 1 and M2_impInterlock7100 == 1 and M2_impInterlock19eg == 1:
                    a = caput(WR_config["联锁状态"],1)
                else:
                    a = caput(WR_config["联锁状态"],0)
                if M1_impOvertime == 0 and M2_impOvertime == 0:
                    a = caput(WR_config["超时状态"],1)
                else:
                    a = caput(WR_config["超时状态"],0)
                if M1_impPhoenix == 1 and M2_impPhoenix == 1:
                    a = caput(WR_config["凤凰状态"],1)
                else:
                    a = caput(WR_config["凤凰状态"],0)
            except Exception as e:
                pass
        except KeyboardInterrupt as e:
            print("Bos_Monitor Thread Stop")
            break

def Node_Monitor_main():
    time.sleep(3)
    print("Node_Monitor Thread Start")
    wrpcTemperatureTable = wrpc.wrpcTemperatureTable()
    wrpcPtpGroup = wrpc.wrpcPtpGroup()
    impCtrlMENA = nod.impCtrlMENA()
    impBPSConfig = nod.impBPSConfig()
    i = 0
    while True:
        time.sleep(0.01)
        error_ip = []
        for pvname,host in Node_raw_data.items():
            if check_ping(host):
                try:
                    if i%10 == 0:
                        Temp = float(wrpcTemperatureTable.get_wrpcTemperatureValue(ip=host)["wrpcTemperatureValue"])
                        a = caput(Device_config["温度"].format(pvname),Temp)
                except:
                    print(wrpcTemperatureTable.get_wrpcTemperatureValue(ip=host))
                    print(f"ip:{host} 获取温度失败")
                try:
                    if i%5 == 0:
                        trigger = int(impBPSConfig.get_impCtrlTrigger(ip=host)["impCtrlTrigger"])
                        if trigger == 1:
                            a = caput(Device_config["触发状态"].format(pvname),0)
                        elif trigger == 11:
                            a = caput(Device_config["触发状态"].format(pvname),1)
                        elif trigger == 10:
                            a = caput(Device_config["触发状态"].format(pvname),2)
                except:
                    print(impBPSConfig.get_impCtrlTrigger(ip=host))
                    print(f"ip:{host} 获取触发状态失败")
                try:
                    if i%60 == 0:
                        enable = int(impCtrlMENA.get_impCtrlMC0Ena(ip=host)["impCtrlMC0Ena"])
                        a = caput(Device_config["使能状态"].format(pvname),enable)
                except:
                    print(wrpcTemperatureTable.get_wrpcTemperatureValue(ip=host))
                    print(f"ip:{host} 获取使能状态失败")

                try:
                    ptpState = int(wrpcPtpGroup.get_wrpcPtpServoStateN(ip=host)["wrpcPtpServoStateN"])
                    if ptpState == 4:
                        a = caput(Device_config["同步状态"].format(pvname),1)
                    else:
                        a = caput(Device_config["同步状态"].format(pvname),0)
                except:
                    a = caput(Device_config["同步状态"].format(pvname),0)
                    print(wrpcPtpGroup.get_wrpcPtpServoStateN(ip=host))
                    print(f"ip:{host} 获取同步状态失败")
            else:
                error_ip.append(host)
                a = caput(Device_config["温度"].format(pvname),0)
                a = caput(Device_config["触发状态"].format(pvname),0)
                a = caput(Device_config["使能状态"].format(pvname),0)
                a = caput(Device_config["同步状态"].format(pvname),0)
        print("设备：\t"+" ".join(error_ip)+"\t连接失败")
            
    
        if i > 60:
            i = 1
        else:
            i += 1